package o;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import com.huawei.appgallery.packagemanager.api.bean.ManagerTask;
import com.huawei.appgallery.packagemanager.impl.uninstallapk.bean.UninstallApkTask;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import o.cof.a;

public class cqe {

    static class d implements FileFilter {
        private d() {
        }

        public boolean accept(File file) {
            return file != null && file.isFile() && file.getName().toLowerCase(Locale.ENGLISH).endsWith(".apk");
        }
    }

    public void ˋ(Context context, Map<String, PackageInfo> map) {
        List arrayList = new ArrayList();
        SharedPreferences sharedPreferences = context.getSharedPreferences("packagemanager_uninstalledapk", 0);
        int i = sharedPreferences.getInt("uninstalledapk_version", 0);
        cqa ˏ = cqa.ˏ(context);
        ˏ.ॱ();
        if (i == 0) {
            ˎ(context, map, arrayList, sharedPreferences);
        } else {
            ˏ(context, map, arrayList);
        }
        cpa.ˊ().ˋ(arrayList);
        ˏ.ˏ();
    }

    private void ˏ(Context context, Map<String, PackageInfo> map, List<ManagerTask> list) {
        for (UninstallApkTask uninstallApkTask : cqa.ˏ(context).ˊ()) {
            if (uninstallApkTask.apkInfos == null) {
                cqa.ˏ(context).ˋ(uninstallApkTask.packageName);
            } else {
                ˋ(context, map, list, uninstallApkTask);
            }
        }
    }

    private void ˋ(Context context, Map<String, PackageInfo> map, List<ManagerTask> list, UninstallApkTask uninstallApkTask) {
        Object obj = 1;
        List arrayList = new ArrayList();
        List arrayList2 = new ArrayList();
        File file = null;
        for (a aVar : uninstallApkTask.apkInfos) {
            File file2;
            Object obj2;
            File file3 = new File(aVar.ˎ);
            arrayList2.add(file3);
            if (file3.exists()) {
                if (ˏ(aVar)) {
                    file2 = file3;
                } else {
                    file2 = file;
                }
                arrayList.add(file3);
                obj2 = obj;
            } else {
                obj2 = null;
                file2 = file;
            }
            file = file2;
            obj = obj2;
        }
        if (obj == null) {
            ˊ(context, uninstallApkTask, arrayList);
            cnu.ˏ.ˏ("CacheDataManagement", "lack of some file, delete them");
        } else if (file == null) {
            cnu.ˏ.ˏ("CacheDataManagement", "can not find master file");
            ˊ(context, uninstallApkTask, arrayList2);
        } else {
            PackageInfo packageInfo = (PackageInfo) map.get(uninstallApkTask.packageName);
            if (packageInfo == null || uninstallApkTask.versionCode > packageInfo.versionCode) {
                ˏ(uninstallApkTask, file);
                list.add(uninstallApkTask);
                return;
            }
            cnu.ˏ.ˏ("CacheDataManagement", packageInfo.packageName + " has high version:" + packageInfo.versionCode);
            ˊ(context, uninstallApkTask, arrayList2);
        }
    }

    private void ˏ(UninstallApkTask uninstallApkTask, File file) {
        if (file.getName().startsWith("predl_")) {
            uninstallApkTask.status = cnz.UNINSTALLED_PREDL;
        } else {
            uninstallApkTask.status = cnz.UNINSTALLED;
        }
    }

    private boolean ˏ(a aVar) {
        return ("base".equals(aVar.ˏ) && 1 == aVar.ॱ) || aVar.ॱ == 0;
    }

    private void ˎ(Context context, Map<String, PackageInfo> map, List<ManagerTask> list, SharedPreferences sharedPreferences) {
        File[] listFiles = context.getFilesDir().listFiles(new d());
        if (listFiles != null) {
            for (File file : listFiles) {
                PackageInfo packageArchiveInfo = context.getPackageManager().getPackageArchiveInfo(file.getPath(), 0);
                if (packageArchiveInfo == null) {
                    cnu.ˏ.ˏ("CacheDataManagement", "not an apk, delete failed:" + file.delete());
                } else {
                    PackageInfo packageInfo = (PackageInfo) map.get(packageArchiveInfo.packageName);
                    if (packageInfo == null || packageArchiveInfo.versionCode > packageInfo.versionCode) {
                        ManagerTask ˏ = ManagerTask.ˏ(new o.cof.d().ˊ(3).ˏ(packageArchiveInfo.versionCode).ˋ(packageArchiveInfo.packageName).ॱ(file.getAbsolutePath(), "base", 0).ˎ());
                        if (file.getName().startsWith("predl_")) {
                            ˏ.status = cnz.UNINSTALLED_PREDL;
                        } else {
                            ˏ.status = cnz.UNINSTALLED;
                        }
                        cqa.ˏ(context).ˎ(ˏ);
                        list.add(ˏ);
                    } else {
                        cnu.ˏ.ˏ("CacheDataManagement", "invalid apk, delete failed:" + file.delete());
                    }
                }
            }
        }
        sharedPreferences.edit().putInt("uninstalledapk_version", 1).commit();
    }

    private void ˊ(Context context, UninstallApkTask uninstallApkTask, List<File> list) {
        for (File delete : list) {
            cnu.ˏ.ˏ("CacheDataManagement", "delete result:" + delete.delete());
        }
        cqa.ˏ(context).ˋ(uninstallApkTask.packageName);
    }
}
